package com.vanke.devops.domain.manager.shangyi.util;

import com.thoughtworks.xstream.XStream;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 陈景阳
 * @since 2017/11/14
 */
public class XStreamTransformer {

    protected static final Map<Class, XStream> CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();

    /**
     * xml -> pojo
     *
     * @param clazz
     * @param xml
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T fromXml(Class<T> clazz, String xml) {
        T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
        return object;
    }

    /**
     * pojo -> xml
     *
     * @param clazz
     * @param object
     * @return
     */
    public static <T> String toXml(Class<T> clazz, T object) {
        return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
    }

    private static Map<Class, XStream> configXStreamInstance() {
        Map<Class, XStream> map = new HashMap<Class, XStream>();
        map.put(String.class,new XStream());
        return map;
    }
}
